const Router = require('koa-router');
const router = new Router();
const path = require('path')
const fs = require('fs')
const { uuid } = require('../libs/common')
const config = require('../config')

router.post('/img', async (ctx) => {
    // 上传单个文件
    const file = ctx.request.files.file // 获取上传文件
    const typeArr = file.type.split('/')
    const fileName = uuid() + '.' + typeArr[typeArr.length - 1]
    // 创建可读流
    const reader = fs.createReadStream(file.path);
    let filePath = config.uploadPath + `/img/${fileName}`;
    // let filePath = path.join(__dirname, '../static/img/') + `${fileName}`;
    console.log(filePath)
    // 创建可写流
    const upStream = fs.createWriteStream(filePath);
    // 可读流通过管道写入可写流
    reader.pipe(upStream);
    return ctx.body = `${config.uploadExport}/img/${fileName}`
})
router.post('/video', async (ctx) => {
    // 上传单个文件
    const file = ctx.request.files.file // 获取上传文件
    const typeArr = file.type.split('/')
    const fileName = uuid() + '.' + typeArr[typeArr.length - 1]
    // 创建可读流
    const reader = fs.createReadStream(file.path);
    let filePath = config.uploadPath + `/video/${fileName}`;
    console.log(filePath)
    // let filePath = path.join(__dirname, '../static/video/') + `${fileName}`;
    // 创建可写流
    const upStream = fs.createWriteStream(filePath);
    // 可读流通过管道写入可写流
    reader.pipe(upStream);
    return ctx.body = `${config.uploadExport}/video/${fileName}`
})

module.exports = router.routes();